/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.servicesimpl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.opu.db_vdumper.beans.UserData;
import org.opu.db_vdumper.services.CopyDbService;
import org.opu.db_vdumper.ui.component.area.model.CanvesModel;
import org.opu.db_vdumper.ui.component.tabel.models.Model;
import org.opu.db_vdumper.ui.component.tabel.models.TableModel;
import org.opu.db_vdumper.util.DbManager;
import org.opu.db_vdumper.util.datacopy.DataMover;
import org.opu.db_vdumper.util.datacopy.Database;
import org.opu.db_vdumper.util.datacopy.DatabaseException;
import org.opu.db_vdumper.util.datacopy.DbResponse;
import org.opu.db_vdumper.util.datacopy.FileDatabase;
import org.opu.db_vdumper.util.monitors.DoubleProgresMonitor;
import org.opu.db_vdumper.util.monitors.SimpleProgresMonitor;

/**
 *
 * @author yura
 */
public class CopyDbServiceImpl implements CopyDbService{

    private DbManager dbManager ;
    private DataMover mover = new DataMover();
    
    private Database target;
    private Database source;

    private List<TableModel> tables;

    public CopyDbServiceImpl(DbManager dbManager, File file) 
            throws DatabaseException, IOException {
        
        this(dbManager);
        target = new FileDatabase(file);
    }

    public CopyDbServiceImpl(DbManager dbManager, UserData dbProp)
            throws DatabaseException {
        
        this(dbManager);
        target = new Database(dbProp.getDbProperties().getClassName(),
                DbManager.getDbUrlString(dbProp), dbProp.getUser(), dbProp.getPassword());
    }

    private CopyDbServiceImpl(DbManager dbManager) throws DatabaseException{
        this.dbManager = dbManager;
        check();
        source = new Database(dbManager.getConnection());
    }

    @Override
    public Map<String, DbResponse> createStructure(CanvesModel model, 
            SimpleProgresMonitor monitor)throws DatabaseException {

        check();
        
        mover.setSource(source, tables = getTables(model));
        mover.setTarget(target);

        return mover.createTables(monitor);
    }

    @Override
    public void copyData(DoubleProgresMonitor monitor) throws DatabaseException {
        check();

        mover.copyTableData(monitor);
    }

    @Override
    public Database getTargetDatabase(){
        return target;
    }

    private void check() throws DatabaseException {
        if (dbManager == null || !dbManager.isConnected()){
            throw new DatabaseException("No connect to source database");
        }
    }

    private List<TableModel> getTables(CanvesModel model){
        List<TableModel> tablesModels = new ArrayList<TableModel>();
        for (Model tm : model.getSelectedComp()) {
            if (tm instanceof TableModel){
                tablesModels.add((TableModel)tm);
            }
        }
        return tablesModels;
    }

}
